package org.leno.houseHire.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.leno.houseHire.service.HouseDTO;
import org.leno.houseHire.service.IHouseService;
import org.leno.houseHire.service.LoginDTO;


public class HouseHandlerAction extends Action {
private IHouseService houseService;
	
	
	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		String action = request.getParameter("action");
		HouseForm hForm = (HouseForm) form;
		HouseDTO house = hForm.getHouse();
		LoginDTO user = (LoginDTO) request.getSession().getAttribute("USER_INFO");
		house.setUserId(user.getUid());
		house.setBooktimeInfo(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		if("addHouse".equals(action)){
			pw.print(1);
			houseService.addHouse(house);
		}else if("editHouse".equals(action)){
			pw.print(1);
			houseService.editHouse(house);
		}
		return null;
	}
}
